package com.guigu.pd.base.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.base.entity.GoodsTypeEntity;
import com.guigu.pd.base.entity.TransportLineTypeEntity;
import com.guigu.pd.base.entity.TransportTripsEntity;
import com.guigu.pd.base.service.TransportLineTypeService;
import com.guigu.pd.base.service.TransportTripsService;
import com.guigu.pd.serviceapi.authfeign.CoreOrgFeign;
import com.guigu.pd.serviceapi.authfeign.UserFeign;
import com.guigu.pd.serviceapi.basefeign.TripsTruckDriverFeign;
import com.guigu.pd.serviceapi.basefeign.TruckFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.Query;

import com.guigu.pd.base.dao.TransportLineDao;
import com.guigu.pd.base.entity.TransportLineEntity;
import com.guigu.pd.base.service.TransportLineService;


@Service("transportLineService")
public class TransportLineServiceImpl extends ServiceImpl<TransportLineDao, TransportLineEntity> implements TransportLineService {


    @Autowired
    private TransportTripsService transportTripsService;

    @Autowired
    TransportLineTypeService transportLineTypeService;

    @Autowired
    CoreOrgFeign coreOrgFeign;

    @Autowired
    TripsTruckDriverFeign tripsTruckDriverFeign;

    @Autowired
    TruckFeign truckFeign;

    @Autowired
    UserFeign userFeign;

    @Override
    public PageVo<TransportLineEntity> querytransportLinefy(Map<String, Object> params) {
        TransportLineEntity
                transportLine = JSON.parseObject(JSON.toJSONString(params),
                TransportLineEntity.class);

        Integer pageno = Integer.parseInt(params.get("pageno").toString());
        Integer pagesize = Integer.parseInt(params.get("pagesize").toString());

        QueryWrapper<TransportLineEntity> queryWrapper = new QueryWrapper<TransportLineEntity>();
        if (StringUtils.isNotBlank(transportLine.getName())) {
            queryWrapper.like("name", transportLine.getName());
        }
        if (StringUtils.isNotBlank(transportLine.getLineNumber())) {
            queryWrapper.like("line_number", transportLine.getLineNumber());
        }
        if (StringUtils.isNotBlank(transportLine.getTransportLineTypeId())) {
            queryWrapper.like("transport_line_type_id", transportLine.getTransportLineTypeId());
        }

        PageHelper.startPage(pageno, pagesize);
        List<TransportLineEntity> list = this.list(queryWrapper);

        //数据查出来后查询下属车次信息
        for (TransportLineEntity transportLineEntity : list) {
            QueryWrapper<TransportTripsEntity> queryWrapper1 = new QueryWrapper<TransportTripsEntity>();
            queryWrapper1.eq("transport_line_id",transportLineEntity.getId());
            BigDecimal estimatedTime = transportLineEntity.getEstimatedTime();
            List<TransportTripsEntity> List = transportTripsService.list(queryWrapper1);
            for (TransportTripsEntity trips : List) {
                String str = tripsTruckDriverFeign.getinfobyId(trips.getId());
                if (str!=null&& str!="") {
                    String[] split = str.split(",");
                    String truckname = truckFeign.gettrucknamebyid(split[0]);
                    String username = userFeign.getuserinfobyId(split[1]);
                    trips.setTruckdriver(truckname + "--" + username);
                }
            }
            transportLineEntity.setTransportTrips(List);
            QueryWrapper<TransportLineTypeEntity> queryWrapper2 = new QueryWrapper<TransportLineTypeEntity>();
            queryWrapper2.eq("id",transportLineEntity.getTransportLineTypeId());
            TransportLineTypeEntity linetype = transportLineTypeService.getOne(queryWrapper2);
            transportLineEntity.setTransportLinename(linetype.getName());
            String startname = coreOrgFeign.getcornamebyid(transportLineEntity.getStartAgencyId());
            String endname = coreOrgFeign.getcornamebyid(transportLineEntity.getEndAgencyId());
            transportLineEntity.setStartAgencyname(startname);
            transportLineEntity.setEndAgencyname(endname);
        }
        PageInfo<TransportLineEntity> pageInfo = new PageInfo<TransportLineEntity>(list);

        PageVo<TransportLineEntity> pageVo = new PageVo<TransportLineEntity>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;

    }
}